/*
 * @Description: 试妆推广H5 mixin
 * @Author: kanglin
 * @Date: 2018-05-11 17:21:40
 * @Copyright: Created by Panxsoft.
 */

var COUPON = '/api/may-promo/promo-coupon';
var USER_INFO_API = '/open-wechat/wechat-user/';
var AUTH_SCOPE = 'snsapi_base'; // snsapi_userinfo 或 snsapi_base 默认 base 如失败则自动跳转 userinfo
var OAUTH_PATH = '/open-wechat/oauth';
var OPEN_ID_API = '/open-wechat/openid';

var mayPromoMixin = {
    methods: {
        /**
         *  解析open id
         */
        resolveOpenId: function () {
            var self = this;
            return new Promise(function (resolve, reject) {
                var url_info = Qs.parse(location.search.replace(/^\?/,''));
                if (url_info.openid) {
                    // alert(url_info.openid);
                    return resolve(url_info.openid);
                } else {
                    return resolve(self.fetchOpenId());
                }
            })
        },

        /**
         *  请求 openid
         */
        fetchOpenId: function () {
            var self = this;
            return axios.get(OPEN_ID_API)
                .then(function (response) {
                    if (parseInt(response.data.code) !== 0) {
                        self.redirectToOAuthPage();
                        throw new Error('open id not found');
                    } else {
                        // console.log('opend',response.data.openid);
                        return response.data.openid;
                    }
                })
        },

        /**
         *  重定向 到
         */
        redirectToOAuthPage() {
            window.location.href = this.assembleOAuthUrl();
        },

        /**
         * 组合参数
         * @return {string}
         */
        assembleOAuthUrl: function() {
            var this_page_url =  encodeURIComponent(window.location.href);
            return OAUTH_PATH
                + '?redirect_url=' + this_page_url
                + '&appid=' + AUTHORIZER_APPID
                + '&scope=' + AUTH_SCOPE
                ;
        },

        /**
         * @param {string} openid
         * @return {object} user_info
         */
        fetchUserInfo: function(openid) {
            // alert(openid+'dss')
            var self = this;
            return axios.get(USER_INFO_API+ openid)
                .then(function (response) {
                    if (parseInt(response.data.code) !== 0) {
                        throw new Error(response.data.error)
                    } else {
                        return  response.data.wechat_user;
                    }
                })
                .catch(function (error) {
                    console.log(error);
                })
        },

        /**
         * 请求卡券列表
         * @param params
         */
        fetchCouponIndex: function(params) {
            var self = this;
            return axios.get(COUPON,{params:params})
                .then(function (response) {
                    if (response.data.code !== 0) {
                        throw new Error(response.data.error);
                    } else {
                        return response.data.coupon_lists;
                    }
                })
                .catch(function (error) {
                    console.log(error);
                })
        },
    }

};
